package com.lovely3x.jobservice.executors.extensions.downloader2.serializer;

import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.lovely3x.jobservice.JobService;
import com.lovely3x.jobservice.executors.extensions.downloader2.DownloadTask;
import com.lovely3x.jobservice.executors.extensions.downloader2.executor.DownloadProgress;
import com.lovely3x.jobservice.serializer.TaskSerializer;
import com.lovely3x.jobservice.task.Progress;
import com.lovely3x.jobservice.task.Task;

import java.util.List;

/**
 * 下载任务序列化器
 * Created by lovely3x on 16-2-2.
 */
public class DownloadTaskSerializer implements TaskSerializer {

    private final Object LOCK = new Object();
    private LiteOrm mLiteOrm;

    @Override
    public void onCreate(JobService jobService) {
        mLiteOrm = LiteOrm.newCascadeInstance(jobService, JobService.DB_NAME);
    }

    @Override
    public void onDestroy() {
        if (mLiteOrm != null) {
            try {
                mLiteOrm.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean serialize(Task task) {
        synchronized (LOCK) {
            return task != null && mLiteOrm != null && mLiteOrm.save(task) > 0;
        }
    }

    @Override
    public boolean serialize(List<? extends Task> tasks) {
        synchronized (LOCK) {
            return tasks != null && !tasks.isEmpty() && mLiteOrm != null && mLiteOrm.save(tasks) > 0;
        }
    }

    @Override
    public boolean serializeProgress(Progress task) {
        synchronized (LOCK) {
            return mLiteOrm.save(task) > 0;
        }
    }

    @Override
    public boolean serializeProgresses(List<? extends Progress> tasks) {
        synchronized (LOCK) {
            return mLiteOrm.save(tasks) > 0;
        }
    }

    @Override
    public List<? extends Task> deserializeTask() {
        synchronized (LOCK) {
            if (mLiteOrm == null) {
                return null;
            }
            return mLiteOrm.query(DownloadTask.class);
        }
    }

    @Override
    public List<? extends Progress> deserializeProgress() {
        synchronized (LOCK) {
            return mLiteOrm.query(DownloadProgress.class);
        }
    }

    @Override
    public boolean deleteTask(long taskWhat) {
        synchronized (LOCK) {
            return mLiteOrm != null && mLiteOrm.delete(WhereBuilder.create(DownloadTask.class).where("_what = ? ", new Object[]{String.valueOf(taskWhat)})) > 0;
        }
    }

    @Override
    public boolean deleteProgress(long taskWhat) {
        synchronized (LOCK) {
            return mLiteOrm.delete(WhereBuilder.create(DownloadProgress.class).where("task_what = ? ", new Object[]{String.valueOf(taskWhat)})) > 0;
        }
    }

    @Override
    public boolean deleteProgresses() {
        synchronized (LOCK) {
            return mLiteOrm.deleteAll(DownloadProgress.class) > 0;
        }
    }

    @Override
    public boolean deleteTasks() {
        synchronized (LOCK) {
            return mLiteOrm != null && mLiteOrm.deleteAll(DownloadTask.class) > 0;
        }
    }
}
